package com.ityj.redis;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtils {

    private static JedisPool jedisPool = null;
    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        Properties properties = new Properties();
        InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("jedis-config.xml");
        try {
            properties.load(is);
            String host = properties.getProperty("Host");
            String port = properties.getProperty("Port");
            String maxTotal = properties.getProperty("MaxTotal");
            String maxIdle = properties.getProperty("MaxIdle");
            jedisPoolConfig.setMaxTotal(Integer.parseInt(maxTotal));
            jedisPoolConfig.setMaxIdle(Integer.parseInt(maxIdle));
            System.out.println(host);
            jedisPool = new JedisPool(jedisPoolConfig,host, Integer.parseInt(port));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
    public static void closeResources(Jedis jedis){
        jedis.close();
    }

}

